
//
//  ZAnimationViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/4/23.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZAnimationViewController.h"
#import "StoreCollectionViewCell.h"
#import "HomeModel.h"
#import "LibCollectionViewCell.h"
#import "ZMoreVideoViewController.h"
#import "ZLibCategoryListViewController.h"
#import "ZGradeViewController.h"
@interface ZAnimationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;


@end
#define kLineNumber 3
#define cellId @"store"
#define LCellId @"libid"

@implementation ZAnimationViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UserModel shareInstance].isComic = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UserModel shareInstance].isComic = false;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.name) {
        [self.navigationItem setItemWithTitle:self.name textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }
    
    self.dataArray = [NSMutableArray array];
    
    [self.view addSubview:self.collectionView];
    NSArray *arr = @[@"icon_xiaoxue",@"icon_zhongxue",@"icon_gaozhong",@"icon_zhigao",@"icon_youxiaoxianjie",@"icon_xiaokao",@"icon_zhongkao",@"icon_gaokao",@"icon_jiuyezhidao",];
    NSArray *titles = @[@"小学",@"中学",@"高中",@"职高",@"幼小衔接",@"小考",@"中考",@"高考",@"就业指导",];
    if ([self.name isEqualToString:@"教辅馆"]) {
        
        for (int i = 0; i<titles.count; i++) {
            HomeModel *model = [HomeModel new];
            model.name = titles[i];
            model.photo = arr[i];
            [self.dataArray addObject:model];
        }
        [self.collectionView reloadData];
    }else{
        [self loadData];
    }
    
    // Do any additional setup after loading the view.
}

//流式布局
- (UICollectionViewFlowLayout *)flowLayout {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //整个 section 的四周边界的margin
    layout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    layout.minimumLineSpacing = 0.f;
    layout.minimumInteritemSpacing = 0.f;
    if ([self.name isEqualToString:@"绘本馆"]) {
        layout.itemSize = CGSizeMake(ScreenWidth/kLineNumber, 125.f);
    }else{
        layout.itemSize = CGSizeMake(ScreenWidth/kLineNumber, 160.f);
    }
    
    //    layout.headerReferenceSize = CGSizeMake(kScreenWidth, SectionHeaderHeight);
    return layout;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-ScreenHeightFlex) collectionViewLayout:[self flowLayout]];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        [self registerCell];
        //        WeakStament(ws);
        //        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        _collectionView.backgroundColor = kWhiteColor;
    }
    return _collectionView;
}

- (void)registerCell {
    
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([StoreCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:cellId];
    
     [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LibCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:LCellId];
    
}


- (void)loadData{
    
    NSString *url;
    if ([self.name isEqualToString:@"动漫馆"]) {
        url = kComicListUrl;
    }else if([self.name isEqualToString:@"绘本馆"]){
        url = kPaintListUrl;
    }else{
        url = kTeachBookUrl;
    }
    [self.dataArray removeAllObjects];
    NSDictionary *dic = @{@"pageIndex":@(1),@"pageSize":@(10)};
    [ZNetworkService getRequestWithBaseUrl:url setParameters:dic success:^(id  _Nonnull result) {
        YYLog(@"%@",result);
//        [AFToast showText:@"数据请求成功"];
        if ([result[@"errorCode"] integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dict in arr) {
                HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
                NSString *url = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo];
                model.picUrl = url;
                [self.dataArray addObject:model];
            }
            [self.collectionView reloadData];
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        YYLog(@"%@",error);
    } delegater:nil];
}

#pragma mark - delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}



-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.name isEqualToString:@"绘本馆"]) {
        LibCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LCellId forIndexPath:indexPath];
        HomeModel *model = self.dataArray[indexPath.row];
        [cell.HeadImage sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:nil];
        return cell;
        
    }else{
        //动漫馆和教材馆
        StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        HomeModel *model = self.dataArray[indexPath.row];
        cell.nameLab.text = model.name;
        
        if (model.picUrl.length>0) {
            [cell.headImg sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:nil];
        }else{
            cell.headImg.image = WDImgName(model.photo);
        }
        
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeModel *model = self.dataArray[indexPath.row];
    
    if ([self.name isEqualToString:@"动漫馆"]) {
        
        ZMoreVideoViewController *more = [ZMoreVideoViewController new];
        more.ttitle = model.name;
        more.cateId = model.ID;
        more.isComicFlag = @"comic";
        [self.navigationController pushViewController:more animated:YES];
        
    }else if ([self.name isEqualToString:@"绘本馆"]){
        
        ZLibCategoryListViewController *cateList = [ZLibCategoryListViewController new];
        cateList.ttitle = model.name;
        cateList.cateId = model.ID;
        [UserModel shareInstance].isLib = YES;
        ///这个和图书馆分类列表 共用一个页面
        cateList.isFlag = @"paint";
        [self.navigationController pushViewController:cateList animated:YES];
        
    }else if ([self.name isEqualToString:@"教材馆"]){
        ///教材馆
        ZLibCategoryListViewController *cateList = [ZLibCategoryListViewController new];
        cateList.ttitle = model.name;
        cateList.cateId = model.ID;
        [UserModel shareInstance].isLib = YES;
        ///这个和图书馆分类列表 共用一个页面
        cateList.isFlag = @"teachBook";
        [self.navigationController pushViewController:cateList animated:YES];
        
    }else{
        ///教辅馆  传参对应  1 2 3 5 6 7 8 9 10
        ZGradeViewController *gr = [ZGradeViewController new];
        [self.navigationController pushViewController:gr animated:YES];
        switch (indexPath.row) {
            case 0:
                {
                    gr.grade = @"1";
                    gr.name = @"小学";
                }
                break;
            case 1:
            {
                gr.grade = @"2";
                gr.name = @"初中";
            }
                break;
            case 2:
            {
                gr.grade = @"3";
                gr.name = @"高中";
            }
                break;
            case 3:
            {
                gr.grade = @"5";
                gr.name = @"职高";
            }
                break;
                
            case 4:
            {
                gr.grade = @"6";
                gr.name = @"幼小衔接";
            }
                break;
            case 5:
            {
                gr.grade = @"7";
                gr.name = @"小考";
            }
                break;
            case 6:
            {
                gr.grade = @"8";
                gr.name = @"中考";
            }
                break;
            case 7:
            {
                gr.grade = @"9";
                gr.name = @"高考";
            }
                break;
            case 8:
            {
                gr.grade = @"10";
                gr.name = @"就业指导";
            }
                break;
                
            default:
                break;
        }
        
    }
}




@end
